﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Text;
using System.Net;
using System.IO;
using mini_crm_Bo;

namespace mini_crm_system
{
    public partial class WiFiCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var cuo = this.Master.FindControl("cuMId") as System.Web.UI.HtmlControls.HtmlInputHidden;
            cuo.Value = "liKh";
            var cuCo = this.Master.FindControl("cuCMId") as System.Web.UI.HtmlControls.HtmlInputHidden;
            cuCo.Value = "liWiFiKh";
            var cuUl = this.Master.FindControl("cuULId") as System.Web.UI.HtmlControls.HtmlInputHidden;
            cuUl.Value = "ulKh";
        }

        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {   // 总是接受    
            return true;
        }

        public DataSet getinfo(string url)
        {
            DataSet ds = new DataSet();
            try
            {

                StringBuilder strResult = new StringBuilder();
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证  
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                //声明一个HttpWebRequest请求 
                request.Timeout = 30000;
                //设置连接超时时间 
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.GetEncoding("GB2312");
                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                strResult.Append(streamReader.ReadToEnd());
                strResult.Insert(0, "<Root>");
                strResult.Append("</Root>");
                Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes(strResult.ToString()));
                ds.ReadXml(s);

                streamReader.Close();
                streamReader.Dispose();
                streamReceive.Close();
                streamReceive.Dispose();
                s.Close();
                s.Dispose();
            }
            catch
            {
                //MessageBox.Show("出错"); 
            }
            return ds;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string reUrl = "https://ps.exands.com/opusins/";
            var ds = getinfo(reUrl);
            CustomerManager cu = new CustomerManager();
            if (cu.ToImportCustomer(ds))
            {
                Alert("导入成功");
                GridView1.DataBind();
            }
        }

        public static void Alert(string message)
        {
            string js = @"<Script language='javascript'> alert('" + message + "');</Script>";
            HttpContext.Current.Response.Write(js);
        }
    }
}